package com.itheima.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author MI
 * @ClassName NacosConfigController.java
 * @createTime 2021年11月25日 19:51:00
 */
@Slf4j
@RefreshScope//动态刷新的注解
@RestController
public class NacosConfigController {
    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @GetMapping("/test-config1")
    public String testConfig() {
        //硬编码方式默认支持动态刷新
        String property = applicationContext
                .getEnvironment().getProperty("config.appName");
        return property;

    }

    @Value("${config.appName}")
    private String appName;
    @Value("${config.env}")
    private String env;

    @GetMapping("/test-config2")
    public String testConfig2() {
        //
        return appName;
    }

    @GetMapping("/test-config3")
    public String testConfig3() {
        return env;
    }
}
